package com.example.housekeeping.service.impl;


import com.example.housekeeping.ex.ServiceException;
import com.example.housekeeping.mapper.UserMapper;
import com.example.housekeeping.pojo.dto.UserAddNewDTO;
import com.example.housekeeping.pojo.dto.UserUpdateDTO;
import com.example.housekeeping.pojo.entity.User;
import com.example.housekeeping.pojo.vo.UserListItemVO;
import com.example.housekeeping.pojo.vo.UserLoginVO;
import com.example.housekeeping.pojo.vo.UserStandardVO;
import com.example.housekeeping.service.UserService;
import com.example.housekeeping.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author bob
 * @since 2023-03-20 19:36:08
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增数据
     *
     * @param userAddNewDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理添加个人信息的业务,参数:{}",userAddNewDTO);
        String name = userAddNewDTO.getUsername();
        long count = userMapper.count(name);
        if (count > 0){
            String message ="添加失败,用户名已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        User user = new User();
        BeanUtils.copyProperties(userAddNewDTO,user);
        log.debug("即将执行插入数据,参数:{}",user);
        userMapper.insert(user);
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除业务,参数:{}",id);
        UserStandardVO user = userMapper.getStandardById(id);
        log.debug("根据ID检查是否存在,查询结果:{}",id,user);
        if (user == null){
            String message ="删除失败,删除的数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
        log.debug("即将执行删除,参数:{}",id);
        userMapper.deleteById(id);
    }

    /**
     * 修改数据
     *
     * @param userUpdateDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void updateById(Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理修改用户信息的业务,ID:{},新数据:{}",id,userUpdateDTO);
        UserStandardVO user = userMapper.getStandardById(id);
        if (user == null){
            String message ="修改用户信息详情失败,尝试修改的用户信息不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        User user1 = new User();
        BeanUtils.copyProperties(userUpdateDTO,user1);
        user1.setId(id);
        userMapper.update(user1);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserStandardVO getStandardById(Long id) {
        log.debug("开始处理根据ID查询个人信息的业务,参数:{}",id);
        UserStandardVO getuser = userMapper.getStandardById(id);
        if (getuser == null) {
            String message = "查询个人信息失败,信息不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return getuser;
    }

    /**
     * 查询列表数据
     *
     * @return 实例对象
     */
    @Override
    public List<UserListItemVO> list() {
        List<UserListItemVO> list = userMapper.list();
        return list;
    }

    @Override
    public UserLoginVO login(UserAddNewDTO userAddNewDTO, HttpSession session) {
        UserLoginVO userByUsername = userMapper.getUserByUsername(userAddNewDTO.getUsername());
        if (userByUsername == null) {
            String message ="用户不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (!userByUsername.getPassword().equals(userAddNewDTO.getPassword())) {
            String message ="密码错误";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        log.debug("注册成功");
        session.setAttribute("user",userByUsername);
        return userByUsername;

    }

    @Override
    public void register(UserAddNewDTO userAddNewDTO) {
        UserLoginVO userByUsername = userMapper.getUserByUsername(userAddNewDTO.getUsername());
        if (userByUsername != null) {
            String message ="用户名已存在，换一个!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

        userMapper.addUser(userAddNewDTO);
    }


}
